﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Workflow.Domain.Enums;
using Workflow.Domain.Models;
using Workflow.Infrastructure.IRepository;
using WorkflowCore.Interface;
using WorkflowCore.Models;
using WorkflowCore.Primitives;

namespace Workflow.Application.Extend
{
    public class ForeachExtend: ContainerStepBody 
    {
        // 审批人员
        public string Collection { get; set; }
        // 是否可并行
        public bool RunParallel { get; set; } = true;
        private readonly IBaseRepository<WorkflowInstanceModel> instanceBase;

        public ForeachExtend(IBaseRepository<WorkflowInstanceModel> instanceBase)
        {
            this.instanceBase = instanceBase;
        }
        public override ExecutionResult Run(IStepExecutionContext context)
        {
            var userIdList = JsonConvert.DeserializeObject<List<long>>(Collection)!;
            if (context.PersistenceData == null)
            {
                //Thread.Sleep(1000);
                //修改流程实例列表
                var instance = (instanceBase.GetValue(x => x.WorkflowInstanceId == context.Workflow.Id)).Result;
                instance.CurrentStepId = context.Step.ExternalId;
                instance.CurrentStepName = context.Step.Name;
                instance.StatuEnum = WorkflowInstanceStatuEnum.Processing;
                instance.CurrentAuditUserIds = Collection;
                //instance.CurrentAuditUsers = AuditUserName.ToString();
                // 待审批的人员ID
                instanceBase.UpdateRange(instance);
                var values = userIdList.Cast<object>().ToList();
                if (!values.Any())
                {
                    return ExecutionResult.Next();
                }
                if (RunParallel)
                {
                    return ExecutionResult.Branch(new List<object>(values),new IteratorPersistenceData { ChildrenActive = true });
                }
                else
                {
                    return ExecutionResult.Branch(new List<object>(new object[]{values.ElementAt(0)}),new IteratorPersistenceData { ChildrenActive = true });
                }
            }
            if (context.PersistenceData is IteratorPersistenceData persistenceData&&persistenceData?.ChildrenActive == true)
            {
                if (context.Workflow.IsBranchComplete(context.ExecutionPointer.Id))
                {
                    if (!RunParallel)
                    {
                        var values = userIdList.Cast<object>();
                        persistenceData.Index++;
                        if (persistenceData.Index < values.Count())
                        {
                            return ExecutionResult.Branch(new List<object>(new object[]{values.ElementAt(persistenceData.Index)}), persistenceData);
                        }
                    }
                return ExecutionResult.Next();
                }
                return ExecutionResult.Persist(persistenceData);
            }
            if (context.PersistenceData is ControlPersistenceData controlPersistenceData &&controlPersistenceData?.ChildrenActive == true)
            {
                if (context.Workflow.IsBranchComplete(context.ExecutionPointer.Id))
                {
                    return ExecutionResult.Next();
                }
            }
            return ExecutionResult.Persist(context.PersistenceData);
        }
    }
}
